<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改效果跟进')" />
    <th:block th:include="include :: bootstrap-fileinput-css"/>
    <th:block th:include="include :: summernote-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-userResultFollow-edit" th:object="${webUserResultFollow}">
            <input name="id" th:field="*{id}" type="hidden">
            <input name="userId" id="userId"  type="hidden" />
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">用户：</label>
                <div class="col-sm-8">
                    <div class="input-group">
                        <input name="nickname" id="nickname" class="form-control" type="text" required autocomplete="off" th:value="*{realname}">
                        <div class="input-group-btn">
                            <button type="button" class="btn btn-white dropdown-toggle" data-toggle="dropdown">
                                <span class="caret"></span>
                            </button>
                            <ul class="dropdown-menu dropdown-menu-right" role="menu">
                            </ul>
                        </div>
                    </div>
                </div>
            </div>

            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">跟进内容：</label>
                <div class="col-sm-8">
                    <input type="hidden" class="form-control" th:field="*{content}">
                    <div class="summernote" id="content"></div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">首次照片：</label>
                <div class="col-sm-8">
                    <input type="hidden" name="img1" th:field="*{img1}">
                    <div class="file-loading">
                        <input class="form-control file-upload" id="img1" name="file" type="file">
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">后期照片：</label>
                <div class="col-sm-8">
                    <input type="hidden" name="img2" th:field="*{img2}">
                    <div class="file-loading">
                        <input class="form-control file-upload" id="img2" name="file" type="file">
                    </div>
                </div>
            </div>
        </form>
    </div>
    <div class="row">
        <div class="col-sm-offset-5 col-sm-10">
            <button type="button" class="btn btn-sm btn-primary" onclick="submitHandler()"><i class="fa fa-check"></i>保 存</button>&nbsp;
            <button type="button" class="btn btn-sm btn-danger" onclick="closeItem()"><i class="fa fa-reply-all"></i>关 闭 </button>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: bootstrap-suggest-js" />
    <th:block th:include="include :: bootstrap-fileinput-js"/>
    <th:block th:include="include :: summernote-js" />
    <script th:inline="javascript">
        var prefix = ctx + "system/userResultFollow";
        $("#form-userResultFollow-edit").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                var userId = $("#nickname").attr("data-id");
                $("#userId").val(userId);
                $.operate.save(prefix + "/edit", $('#form-userResultFollow-edit').serialize());
            }
        }

        $(".file-upload").each(function (i) {
            var val = $("input[name='" + this.id + "']").val()
            $(this).fileinput({
                'uploadUrl': ctx + 'common/upload',
                initialPreviewAsData: true,
                initialPreview: [val],
                maxFileCount: 1,
                autoReplace: true
            }).on('fileuploaded', function (event, data, previewId, index) {
                $("input[name='" + event.currentTarget.id + "']").val(data.response.url)
            }).on('fileremoved', function (event, id, index) {
                $("input[name='" + event.currentTarget.id + "']").val('')
            })
            $(this).fileinput('_initFileActions');
        });

        $(function() {
            $('.summernote').each(function(i) {
                $('#' + this.id).summernote({
                    lang: 'zh-CN',
                    height:160,
                    callbacks: {
                        onChange: function(contents, $edittable) {
                            $("input[name='" + this.id + "']").val(contents);
                        },
                        onImageUpload: function(files) {
                            var obj = this;
                            var data = new FormData();
                            data.append("file", files[0]);
                            $.ajax({
                                type: "post",
                                url: ctx + "common/upload",
                                data: data,
                                cache: false,
                                contentType: false,
                                processData: false,
                                dataType: 'json',
                                success: function(result) {
                                    if (result.code == web_status.SUCCESS) {
                                        $('#' + obj.id).summernote('insertImage', result.url);
                                    } else {
                                        $.modal.alertError(result.msg);
                                    }
                                },
                                error: function(error) {
                                    $.modal.alertWarning("图片上传失败。");
                                }
                            });
                        }
                    }
                });
                var content = $("input[name='" + this.id + "']").val();
                $('#' + this.id).summernote('code', content);
            })
        });

        var users = [[${users}]];
        if(typeof users =="string"){
            users = eval(users);
        }

        var values=[];
        for(var index in users){
            var val = {};
            val.userId = users[index].id;
            var realname = "-";
            if(users[index].nickname){
               realname = users[index].nickname+"-";
            }
            if(users[index].realname){
              realname += users[index].realname;
            }
            if(realname != "-"){
                val.realname = realname;
                values.push(val);
            }
        }
        //data 数据中获取
        $("#nickname").bsSuggest({
            idField: "userId",
            keyField: "realname",
            data: {
                'value': values,
                'defaults': 'http://www.baidu.vip'
            }
        });
    </script>
</body>
</html>